package mapspinner.util;

import java.net.URL;

import com.jme.renderer.Renderer;
import com.jme.renderer.pass.Pass;
import com.jme.scene.Spatial;
import com.jme.scene.Spatial.LightCombineMode;
import com.jme.scene.Spatial.NormalsMode;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.CullState;
import com.jme.scene.state.GLSLShaderObjectsState;
import com.jme.scene.state.ZBufferState;
import com.jme.scene.state.BlendState.BlendEquation;
import com.jme.scene.state.BlendState.DestinationFunction;
import com.jme.scene.state.BlendState.SourceFunction;
import com.jme.scene.state.CullState.Face;
import com.jme.scene.state.ZBufferState.TestFunction;
import com.jme.system.DisplaySystem;

public class GrassRenderPass extends Pass {
	public static String shaderDirectory = "mapspinner/util/shaders/";
	private static final long serialVersionUID = 1L;
	private boolean supported = true;
	private final DisplaySystem display;
	private final GLSLShaderObjectsState shader;
	private final CullState cullBackFace;

	protected URL vertexShader;
	protected URL fragmentShader;

	public GrassRenderPass() {

		resetParameters();

		display = DisplaySystem.getDisplaySystem();

		shader = display.getRenderer().createGLSLShaderObjectsState();

		if (!GLSLShaderObjectsState.isSupported())
			supported = false;
		else {
		}

		cullBackFace = display.getRenderer().createCullState();
		cullBackFace.setEnabled(true);
		cullBackFace.setCullFace(Face.Back);

		if (isSupported()) {
			shader.load(vertexShader, fragmentShader);
			shader.setEnabled(true);
		} else
			supported = false;

	}

	@Override
	protected void doRender(final Renderer r) {
		if (isSupported()) {
			shader.clearUniforms();
			shader.setUniform("tex0", 0);
			shader.apply();
		}
	}

	@Override
	protected void doUpdate(final float tpf) {
		super.doUpdate(tpf);
	}

	public boolean isSupported() {
		return supported;
	}

	public void resetParameters() {
		vertexShader = getClass().getClassLoader().getResource(
				TerrainRenderPass4.shaderDirectory + "grass_vert.glsl");
		fragmentShader = getClass().getClassLoader().getResource(
				TerrainRenderPass4.shaderDirectory + "grass_frag.glsl");
	}

	public void setShadingOnSpatial(final Spatial spatial) {
		spatial.setRenderState(cullBackFace);
		if (isSupported()) {
			spatial.setRenderState(shader);
			final ZBufferState zs = DisplaySystem.getDisplaySystem()
					.getRenderer().createZBufferState();
			final BlendState bs = DisplaySystem.getDisplaySystem()
					.getRenderer().createBlendState();
			bs.setTestEnabled(true);
			bs.setEnabled(true);
			bs.setBlendEnabled(true);
			bs.setSourceFunctionAlpha(SourceFunction.OneMinusSourceAlpha);
			bs.setDestinationFunctionAlpha(DestinationFunction.SourceAlpha);
			bs.setBlendEquationAlpha(BlendEquation.Max);
			zs.setEnabled(true);
			zs.setWritable(false);
			zs.setFunction(TestFunction.LessThan);
			spatial.setRenderState(zs);
			spatial.setRenderState(bs);
			spatial.setLightCombineMode(LightCombineMode.Off);
			spatial.setNormalsMode(NormalsMode.Off);
		}
		spatial.updateRenderState();
	}
}